extern "C" {
#include <sys/inotify.h>
#include <sys/epoll.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h> //for stat
#include <sys/types.h>
#include <dirent.h>

#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <ngx_event.h>
#include <ngx_event_connect.h>
}

#include "blog_home_page.h"
#include "ngx_blog.h"
#include "lib_markdown_parse.h"

#include <fstream>

using namespace std;

static void* ngx_http_blog_create_main_conf(ngx_conf_t* cf);
static ngx_int_t ngx_http_blog_all(ngx_cycle_t *cycle);
static ngx_int_t ngx_http_blog_init(ngx_conf_t *cf);
static ngx_int_t ngx_http_blog_handler(ngx_http_request_t *r);
static ngx_int_t ngx_http_blog_get_wd_str(ngx_array_t* wd_dir_array,ngx_int_t key,
                                          ngx_str_t* ret_str,ngx_http_request_t *r);
static string strim_last_slash(string name);
static bool is_directory(string name);
static bool delete_html_file(string name);
static bool copy_file(string src_dir,string src_file,
                       string dst_dir);
static bool copy_directory(string src_dir,string dst_src);
static bool get_all_subdir(string dir_with_path,vector<string>* subdir_without_path);
static bool create_all_output_dir(string output_pre_with_path,
                           string output_blog_without_path,
                           vector<string> output_blog_sub_without_path,
                           string output_archive_without_path,
                           string output_tags_without_path);
static bool create_nginx_blog_without_md(string md_file_dir_with_path,
                       string html_title,
                       string blog_name,
                       string blog_subname,
                       int max_item_per_page,
                       blog_home_page* blog_home);
static bool output_index_html(string output_dir_with_path,
                       int max_item_per_page,
                       blog_home_page* blog_home);
static bool output_archive_html(string output_dir_with_path,
                         int max_item_per_page,
                         blog_home_page* blog_home);
static bool output_tags_html(string output_dir_with_path,
                      blog_home_page* blog_home);
static bool create_output_all_md_blog_html(string output_dir_with_path,
                          string html_title,
                          string blog_name,
                          string blog_subname,
                          blog_home_page* blog_home);
static bool create_out_a_md_blog_html(string src_dir_with_path,
                   string md_name_with_post,
                   string html_title,
                   string blog_name,
                   string blog_subname,
                   string dst_dir_with_path,
                   blog_home_page* blog_home);

static bool create_out_about_me_html(string src_dir_with_path,
                                      string md_name_with_post,
                                      string html_title,
                                      string blog_name,
                                      string blog_subname,
                                      string dst_dir_with_path,
                                      blog_home_page* blog_home);

static ngx_command_t ngx_http_blog_commands[] = {
    { ngx_string("src_root_dir"),
      NGX_HTTP_MAIN_CONF| NGX_CONF_TAKE1,
      ngx_conf_set_str_slot,
      NGX_HTTP_MAIN_CONF_OFFSET,
      offsetof(ngx_http_blog_main_conf_t,src_root_dir),
      NULL },

    { ngx_string("dst_root_dir"),
      NGX_HTTP_MAIN_CONF| NGX_CONF_TAKE1,
      ngx_conf_set_str_slot,
      NGX_HTTP_MAIN_CONF_OFFSET,
      offsetof(ngx_http_blog_main_conf_t,dst_root_dir),
      NULL },

     { ngx_string("html_title"),
      NGX_HTTP_MAIN_CONF| NGX_CONF_TAKE1,
      ngx_conf_set_str_slot,
      NGX_HTTP_MAIN_CONF_OFFSET,
      offsetof(ngx_http_blog_main_conf_t,html_title),
      NULL },

    { ngx_string("blog_name"),
      NGX_HTTP_MAIN_CONF| NGX_CONF_TAKE1,
      ngx_conf_set_str_slot,
      NGX_HTTP_MAIN_CONF_OFFSET,
      offsetof(ngx_http_blog_main_conf_t,blog_name),
      NULL },

    { ngx_string("blog_subname"),
      NGX_HTTP_MAIN_CONF| NGX_CONF_TAKE1,
      ngx_conf_set_str_slot,
      NGX_HTTP_MAIN_CONF_OFFSET,
      offsetof(ngx_http_blog_main_conf_t,blog_subname),
      NULL },

     { ngx_string("max_item_per_page"),
      NGX_HTTP_MAIN_CONF| NGX_CONF_TAKE1,
      ngx_conf_set_num_slot,
      NGX_HTTP_MAIN_CONF_OFFSET,
      offsetof(ngx_http_blog_main_conf_t,max_item_per_page),
      NULL },


    ngx_null_command

};

static ngx_http_module_t  ngx_http_blog_module_ctx = {
    NULL,                                  /* preconfiguration */
    ngx_http_blog_init,                    /* postconfiguration */

    ngx_http_blog_create_main_conf,        /* create main configuration */
    NULL,                                  /* init main configuration */

    NULL,                                  /* create server configuration */
    NULL,                                  /* merge server configuration */

    NULL,                                  /* create location configuration */
    NULL                                   /* merge location configuration */
};

ngx_module_t  ngx_http_blog_module = {
    NGX_MODULE_V1,
    &ngx_http_blog_module_ctx,             /* module context */
    ngx_http_blog_commands,                /* module directives */
    NGX_HTTP_MODULE,                       /* module type */
    NULL,                                  /* init master */
    ngx_http_blog_all,                     /* init module */
    NULL,                                  /* init process */
    NULL,                                  /* init thread */
    NULL,                                  /* exit thread */
    NULL,                                  /* exit process */
    NULL,                                  /* exit master */
    NGX_MODULE_V1_PADDING
};

static void* ngx_http_blog_create_main_conf(ngx_conf_t *cf){
   ngx_http_blog_main_conf_t *conf;
   conf = (ngx_http_blog_main_conf_t*)ngx_palloc(cf->pool,sizeof(ngx_http_blog_main_conf_t));
   if(conf == NULL){
      return NULL;
   }

   conf->max_item_per_page = NGX_CONF_UNSET_UINT;
   conf->inotify_fd = NGX_CONF_UNSET_UINT;
   conf->wd_dir_key_val = NULL;

   //create the space to store the wd and the directory
   ngx_array_t* a;
   a = (ngx_array_t*)ngx_array_create(cf->pool,4,sizeof(wd_dir_key_val_t));
   if(a == NULL){
       return NULL;
   }
   conf->wd_dir_key_val = a;

   return conf;
}

static ngx_int_t ngx_http_blog_all(ngx_cycle_t *cycle){
    ngx_http_blog_main_conf_t *bmcf;
    bmcf = (ngx_http_blog_main_conf_t*)ngx_http_cycle_get_module_main_conf(cycle,ngx_http_blog_module);

    string src_root_dir = string(reinterpret_cast<char*>(bmcf->src_root_dir.data));
    string dst_root_dir = string(reinterpret_cast<char*>(bmcf->dst_root_dir.data));
    string html_title = string(reinterpret_cast<char*>(bmcf->html_title.data));
    string blog_name = string(reinterpret_cast<char*>(bmcf->blog_name.data));
    string blog_subname = string(reinterpret_cast<char*>(bmcf->blog_subname.data));
    int max_item_per_page = bmcf->max_item_per_page;

    string src_blog_dir = string("blog/");
    string dst_blog_dir = string("blog/");
    string dst_archive_dir = string("archive/");
    string dst_tags_dir = string("tags/");

    vector<string> src_blog_subdir_without_path;
    if(!get_all_subdir(src_root_dir + src_blog_dir,&src_blog_subdir_without_path)){
        return NGX_ERROR;
    }

    if(!create_all_output_dir(dst_root_dir,
                             dst_blog_dir,
                             src_blog_subdir_without_path,
                             dst_archive_dir,
                             dst_tags_dir)){
        return NGX_ERROR;
    }

    blog_home_page blog_home;

    if(!create_nginx_blog_without_md(src_root_dir + src_blog_dir,
                                     html_title,
                                     blog_name,
                                     blog_subname,
                                     max_item_per_page,
                                     &blog_home)){
        return NGX_ERROR;
    }


    if(!output_index_html(dst_root_dir,
                          max_item_per_page,
                          &blog_home)){
        return NGX_ERROR;
    }

    if(!output_archive_html(dst_root_dir + dst_archive_dir,
                            max_item_per_page,
                            &blog_home)){
        return NGX_ERROR;
    }

    if(!output_tags_html(dst_root_dir + dst_tags_dir,
                         &blog_home)){
        return NGX_ERROR;
    }

    if(!create_output_all_md_blog_html(dst_root_dir + dst_blog_dir,
                                       html_title,
                                       blog_name,
                                       blog_subname,
                                       &blog_home)){
        return NGX_ERROR;
    }
    //create about_me.html
    if(!create_out_about_me_html(src_root_dir,
                                  string("about_me.md"),
                                  html_title,
                                  blog_name,
                                  blog_subname,
                                  dst_root_dir,
                                  &blog_home)){
        return NGX_OK;
    }


    vector<string>::iterator it;
    for(it = src_blog_subdir_without_path.begin();
        it != src_blog_subdir_without_path.end();
        it++){
        if(!copy_directory(src_root_dir+src_blog_dir + (*it),
                           dst_root_dir + dst_blog_dir + (*it))){
            return false;
        }
    }

    blog_home.clear_all();

    int inotify_fd = inotify_init1(IN_NONBLOCK);
    if(inotify_fd < 0){
        cout << "init inotify error\n";
        return NGX_ERROR;
    }

    bmcf->inotify_fd = inotify_fd;

    int op = IN_CREATE | IN_MODIFY | IN_DELETE;
    wd_dir_key_val_t* wd_dir_key_val;

    if((bmcf->wd[0] = inotify_add_watch(inotify_fd,
                                  (strim_last_slash(src_root_dir + src_blog_dir)).c_str()
                                  ,op)) == -1){
        cout << "can not add inotify watch " << (src_root_dir + src_blog_dir) << endl;
        close(inotify_fd);
        return NGX_ERROR;
    }
    else{
        wd_dir_key_val = (wd_dir_key_val_t*)ngx_array_push(bmcf->wd_dir_key_val);
        wd_dir_key_val->wd = bmcf->wd[0];
        wd_dir_key_val->dir_or_file.data = (u_char*)ngx_palloc(cycle->pool,64);
        strcpy((char*)wd_dir_key_val->dir_or_file.data,(src_blog_dir).c_str());
        wd_dir_key_val->dir_or_file.len = src_blog_dir.length()+1;
    }

    if((bmcf->wd[1] = inotify_add_watch(inotify_fd,
                                  (strim_last_slash(src_root_dir)).c_str()
                                  ,op)) == -1){
        cout << "can not add inotify watch " << (src_root_dir) << endl;
        close(inotify_fd);
        return NGX_ERROR;
    }
    else{
        wd_dir_key_val = (wd_dir_key_val_t*)ngx_array_push(bmcf->wd_dir_key_val);
        wd_dir_key_val->wd = bmcf->wd[1];
        wd_dir_key_val->dir_or_file.data = (u_char*)ngx_palloc(cycle->pool,64);
        strcpy((char*)wd_dir_key_val->dir_or_file.data,(string("")).c_str());
        wd_dir_key_val->dir_or_file.len = 1;
    }

    int i = 1;
    int wr;
    for(it = src_blog_subdir_without_path.begin();
        it != src_blog_subdir_without_path.end();
        it++){
        if((wr = inotify_add_watch(inotify_fd,
                                      (strim_last_slash(src_root_dir + src_blog_dir + (*it))).c_str(),op)) == -1){
            cout << "can not add inotify watch " << src_root_dir + src_blog_dir + (*it) << endl;
            close(inotify_fd);
            return NGX_ERROR;
        }
        else{
            wd_dir_key_val = (wd_dir_key_val_t*)ngx_array_push(bmcf->wd_dir_key_val);
            wd_dir_key_val->wd = wr;


            wd_dir_key_val->dir_or_file.data = (u_char*)ngx_palloc(cycle->pool,64);
            strcpy((char*)wd_dir_key_val->dir_or_file.data,(*it).c_str());
            wd_dir_key_val->dir_or_file.len = (*it).length()+1;
            ngx_str_set(&(wd_dir_key_val->dir_or_file),const_cast<unsigned char*>(reinterpret_cast<const unsigned char*>(((*it)).c_str())));
        }
        i++;
    }
    //end to add to inotify
    return NGX_OK;
}

static ngx_int_t ngx_http_blog_init(ngx_conf_t *cf){
    ngx_http_handler_pt*        h;
    ngx_http_core_main_conf_t*  cmcf;

    cmcf = (ngx_http_core_main_conf_t*)ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);

    h = (ngx_http_handler_pt*)ngx_array_push(&cmcf->phases[NGX_HTTP_ACCESS_PHASE].handlers);
    if (h == NULL) {
        return NGX_ERROR;
    }

    *h = ngx_http_blog_handler;

    return NGX_OK;
}

static ngx_int_t ngx_http_blog_handler(ngx_http_request_t *r){
    ngx_http_blog_main_conf_t *bmcf;
    bmcf = (ngx_http_blog_main_conf_t*)ngx_http_get_module_main_conf(r,ngx_http_blog_module);

    string src_root_dir = string(reinterpret_cast<char*>(bmcf->src_root_dir.data));
    string dst_root_dir = string(reinterpret_cast<char*>(bmcf->dst_root_dir.data));
    string html_title = string(reinterpret_cast<char*>(bmcf->html_title.data));
    string blog_name = string(reinterpret_cast<char*>(bmcf->blog_name.data));
    string blog_subname = string(reinterpret_cast<char*>(bmcf->blog_subname.data));
    int max_item_per_page = bmcf->max_item_per_page;

    string src_blog_dir = string("blog/");
    string dst_blog_dir = string("blog/");
    string dst_archive_dir = string("archive/");
    string dst_tags_dir = string("tags/");

    string about_me = string("about_me.md");

    blog_home_page blog_home;
    string src_dir;
    string src_file;
    string dst_dir;
    string sub_dir;

    int op = IN_CREATE | IN_MODIFY | IN_DELETE;

    wd_dir_key_val_t* wd_dir_key_val;
    int length;
    char inotify_event_buf[1024];

    while((length = read(bmcf->inotify_fd,inotify_event_buf,1024)) > 0){
        blog_home.clear_all();
        struct inotify_event* iev = (struct inotify_event*)inotify_event_buf;

        //blog dir create directory,add the diretory into the inotify_fd to watch
        //blog dir create md file,call nginx_blog_md and nginx_blog_other
        //blog dir modify or close write md file,call  nginx_blog_md and nginx_blog_other
        //else blog subdir create modify or close write file,copy changed files into the html directory
        if(iev->wd == bmcf->wd[0]){
            if(iev->mask & IN_CREATE){

                if(is_directory(src_root_dir + src_blog_dir + string(iev->name))){
                    int new_subdir = inotify_add_watch(bmcf->inotify_fd,
                                                       (src_root_dir + src_blog_dir + string(iev->name)).c_str(),op);
                    if(new_subdir == -1){
                        ngx_log_error(NGX_LOG_ALERT,r->connection->log,0,"can not add the new subdir into the inotify\n") ;
                    }
                    else{
                        wd_dir_key_val = (wd_dir_key_val_t*)ngx_array_push(bmcf->wd_dir_key_val);
                        wd_dir_key_val->wd = new_subdir;

                        wd_dir_key_val->dir_or_file.data = (u_char*)ngx_palloc(r->pool,64);
                        strcpy((char*)wd_dir_key_val->dir_or_file.data,(string(iev->name) + string("/")).c_str());
                        wd_dir_key_val->dir_or_file.len = (string(iev->name) + string("/")).length();

                       // wd_dir_key_val->dir_or_file.data = const_cast<unsigned char*>(reinterpret_cast<const unsigned char*>((string(iev->name) + string("/")).c_str()));
                       // wd_dir_key_val->dir_or_file.len = (string(iev->name) + string("/")).length();
                        if(!create_dir(dst_root_dir + dst_blog_dir + string(iev->name) + string("/"))){
                            cout << "can not create dir:" << dst_root_dir + dst_blog_dir + string(iev->name) << endl;
                            return NGX_OK;
                        }
                    }
                }
                if(is_md_file(string(iev->name))){
                    if(!create_nginx_blog_without_md(src_root_dir + src_blog_dir,
                                                     html_title,
                                                     blog_name,
                                                     blog_subname,
                                                     max_item_per_page,
                                                     &blog_home)){
                        return NGX_OK;
                    }


                    if(!output_index_html(dst_root_dir,
                                          max_item_per_page,
                                          &blog_home)){
                        return NGX_OK;
                    }

                    if(!output_archive_html(dst_root_dir + dst_archive_dir,
                                            max_item_per_page,
                                            &blog_home)){
                        return NGX_OK;
                    }

                    if(!output_tags_html(dst_root_dir + dst_tags_dir,
                                         &blog_home)){
                        return NGX_OK;
                    }

                    if(!create_out_a_md_blog_html(src_root_dir + src_blog_dir,
                                                  string(iev->name),
                                                  html_title,
                                                  blog_name,
                                                  blog_subname,
                                                  dst_root_dir + dst_blog_dir,
                                                  &blog_home)){
                        return NGX_OK;
                    }
                }
            }
            if(iev->mask & IN_MODIFY){
                if(is_md_file(string(iev->name))){
                    if(!create_nginx_blog_without_md(src_root_dir + src_blog_dir,
                                                     html_title,
                                                     blog_name,
                                                     blog_subname,
                                                     max_item_per_page,
                                                     &blog_home)){
                        return NGX_OK;
                    }


                    if(!output_index_html(dst_root_dir,
                                          max_item_per_page,
                                          &blog_home)){
                        return NGX_OK;
                    }

                    if(!output_archive_html(dst_root_dir + dst_archive_dir,
                                            max_item_per_page,
                                            &blog_home)){
                        return NGX_OK;
                    }

                    if(!output_tags_html(dst_root_dir + dst_tags_dir,
                                         &blog_home)){
                        return NGX_OK;
                    }

                    if(!create_out_a_md_blog_html(src_root_dir + src_blog_dir,
                                                  string(iev->name),
                                                  html_title,
                                                  blog_name,
                                                  blog_subname,
                                                  dst_root_dir + dst_blog_dir,
                                                  &blog_home)){
                        return NGX_OK;
                    }
                }
            }
            if(iev->mask & IN_DELETE){
                string iev_name = string(iev->name);
                int pos = iev_name.find_last_of(".");
                string html_file = iev_name.substr(0,pos) + string(".html");
                if(!delete_html_file(dst_root_dir + dst_blog_dir + html_file)){
                    cout << "can not delete file:" << iev_name << endl;
                }
                vector<string> src_blog_subdir_without_path;
                if(!get_all_subdir(src_root_dir + src_blog_dir,&src_blog_subdir_without_path)){
                    return NGX_OK;
                }


                if(!create_nginx_blog_without_md(src_root_dir + src_blog_dir,
                                                 html_title,
                                                 blog_name,
                                                 blog_subname,
                                                 max_item_per_page,
                                                 &blog_home)){
                    return NGX_OK;
                }


                if(!output_index_html(dst_root_dir,
                                      max_item_per_page,
                                      &blog_home)){
                    return NGX_OK;
                }

                if(!output_archive_html(dst_root_dir + dst_archive_dir,
                                        max_item_per_page,
                                        &blog_home)){
                    return NGX_OK;
                }

                if(!output_tags_html(dst_root_dir + dst_tags_dir,
                                     &blog_home)){
                    return NGX_OK;
                }

                //for md files managed by git
                if(!create_out_a_md_blog_html(src_root_dir + src_blog_dir,
                                              string(iev->name),
                                              html_title,
                                              blog_name,
                                              blog_subname,
                                              dst_root_dir + dst_blog_dir,
                                              &blog_home)){
                    return NGX_OK;
                }
            }
        }
        else if(iev->wd == bmcf->wd[1]){
            if((iev->mask & IN_CREATE ) || (iev->mask & IN_MODIFY)){
                if(strcasecmp(about_me.c_str(),iev->name) == 0){
                    if(!create_nginx_blog_without_md(src_root_dir + src_blog_dir,
                                                     html_title,
                                                     blog_name,
                                                     blog_subname,
                                                     max_item_per_page,
                                                     &blog_home)){
                        return NGX_OK;
                    }

                    //create about_me.html
                    if(!create_out_about_me_html(src_root_dir,
                                                 string("about_me.md"),
                                                 html_title,
                                                 blog_name,
                                                 blog_subname,
                                                 dst_root_dir,
                                                 &blog_home)){
                        ngx_log_error(NGX_LOG_ALERT,r->connection->log,0,"false");
                        return NGX_OK;
                    }
                    ngx_log_error(NGX_LOG_ALERT,r->connection->log,0,"true");
                }
            }
            //for md files managed by git
            else{
                if(strcasecmp(about_me.c_str(),iev->name) == 0){
                    if(!create_nginx_blog_without_md(src_root_dir + src_blog_dir,
                                                     html_title,
                                                     blog_name,
                                                     blog_subname,
                                                     max_item_per_page,
                                                     &blog_home)){
                        return NGX_OK;
                    }

                    //create about_me.html
                    if(!create_out_about_me_html(src_root_dir,
                                                 string("about_me.md"),
                                                 html_title,
                                                 blog_name,
                                                 blog_subname,
                                                 dst_root_dir,
                                                 &blog_home)){
                        ngx_log_error(NGX_LOG_ALERT,r->connection->log,0,"false");
                        return NGX_OK;
                    }
                    ngx_log_error(NGX_LOG_ALERT,r->connection->log,0,"true");
                }
            }
        }
        else{
            if(iev->mask & IN_CREATE){
                ngx_str_t val;
                if(ngx_http_blog_get_wd_str(bmcf->wd_dir_key_val,iev->wd,&val,r) == NGX_ERROR){
                    return NGX_OK;
                }

                if(val.data != NULL){
                   sub_dir = string(reinterpret_cast<char*>(val.data));
                }

                if(!is_directory(src_root_dir + src_blog_dir + sub_dir + string(iev->name))){

                    src_dir.clear();
                    src_dir.shrink_to_fit();
                    src_file.clear();
                    src_file.shrink_to_fit();
                    dst_dir.clear();
                    dst_dir.shrink_to_fit();


                    src_dir = src_root_dir + src_blog_dir + sub_dir;


                    src_file = string(iev->name);
                    dst_dir = dst_root_dir + dst_blog_dir + sub_dir;
                    if(!copy_file(src_dir,src_file,dst_dir)){

                        cout << "copy changed file error\n"
                             << "the src dir is:" << src_dir
                             << "the src file is:" << src_file
                             << "the dst dir is:" << dst_dir;
                    }
                }
            }
        }
    }
    if(length == -1 && errno != EAGAIN){
        cout << "some error happened when read from the inotify fd\n";
    }


    return NGX_OK;
}

static ngx_int_t ngx_http_blog_get_wd_str(ngx_array_t* wd_dir_array,ngx_int_t key,
                                          ngx_str_t* ret_str,ngx_http_request_t *r){

   for(ngx_uint_t i = 0;i < wd_dir_array->nelts;i++){
       wd_dir_key_val_t* wd_dir_key_val = (wd_dir_key_val_t*)((char*)wd_dir_array->elts + sizeof(wd_dir_key_val_t)*i);
       if(wd_dir_key_val->wd == key){
           ret_str->data = wd_dir_key_val->dir_or_file.data;
           ret_str->len = wd_dir_key_val->dir_or_file.len;
           return NGX_OK;
       }
   }
   return NGX_ERROR;
}


static string strim_last_slash(string name){
    int pos = name.find_last_of("/");
    name = name.substr(0,pos);
    return name;
}

static bool is_directory(string name){
    struct stat stat_buf;
    if(stat(name.c_str(),&stat_buf) == -1){
        cout << "get stat of " << name << " wrong\n";
        return false;
    }
    if((stat_buf.st_mode & S_IFMT) == S_IFDIR){
        return true;
    }
    return false;
}

static bool delete_html_file(string name){
    //file do not exist ,return true
    if(access(name.c_str(),F_OK) == -1){
        return true;
    }

    if(remove(name.c_str()) == 0){
        return true;
    }
    return false;
}


static bool copy_file(string src_dir,string src_file,
                      string dst_dir){
    //copy  file


    ifstream file_read;
    ofstream file_write;



    file_read.open(src_dir+src_file,fstream::binary);
    if(!file_read){
        cout << " 1 can not open  file " << src_dir + src_file << endl;
        return false;
    }
    string file_name = dst_dir + src_file;
    file_write.open(file_name,fstream::binary);
    if(!file_write){
        cout << " 2 can not open img file " << file_name << endl;
        return false;
    }

    if (file_read) {
        // get length of file:
        file_read.seekg (0, file_read.end);
        int length = file_read.tellg();
        file_read.seekg (0, file_read.beg);

        char * buffer = new char [length];

        // read data as a block:
        file_read.read (buffer,length);

        if (!file_read){
            cout << "error: only " << file_read.gcount() << " could be read";
            return false;
        }
        file_write.write(buffer,length);
        delete []buffer;

    }

    file_read.close();
    file_write.close();

    return true;
    //end copy file
}


static bool copy_directory(string src_dir,string dst_src){
    DIR *dir;
    dir = opendir(src_dir.c_str());
    if(dir == NULL){
        cout << "error open dir " << src_dir << endl;
        return false;
    }

    struct dirent* ptr;
    while((ptr = readdir(dir)) != NULL){
        if(strcmp(ptr->d_name,".") == 0 || strcmp(ptr->d_name,"..") == 0){
            continue;
        }
        else if(ptr->d_type == DT_REG){
            if(!copy_file(src_dir,string(ptr->d_name),dst_src)){
                cout << "copy file " << ptr->d_name << " from directory "
                     << src_dir << " to directory " << dst_src << endl;
            }
        }
        else{
            continue;
        }
    }
    return true;
}

static bool get_all_subdir(string dir_with_path,vector<string>* subdir_without_path){
    DIR* dir;
    struct dirent* ptr;

    dir = opendir(dir_with_path.c_str());
    if(dir == NULL){
        cout << "error:get all subdir of " << dir_with_path << endl;
        return false;
    }

    while((ptr = readdir(dir)) != NULL){
        if(strcmp(ptr->d_name,".") == 0 || strcmp(ptr->d_name,"..") == 0){
            continue;
        }
        else if(ptr->d_type == DT_DIR){
            subdir_without_path->push_back(string(ptr->d_name) + string("/"));
        }
        else{
            continue;
        }
    }
    return true;
}

static bool create_all_output_dir(string output_pre_with_path,
                                  string output_blog_without_path,
                                  vector<string> output_blog_sub_without_path,
                                  string output_archive_without_path,
                                  string output_tags_without_path){
    if(!create_dir(output_pre_with_path)){
        return false;
    }
    if(!create_dir(output_pre_with_path + output_blog_without_path)){
        return false;
    }

    vector<string>::iterator it;
    for(it = output_blog_sub_without_path.begin();
        it != output_blog_sub_without_path.end();
        it++){
        if(!create_dir(output_pre_with_path + output_blog_without_path + (*it))){
            return false;
        }
    }

    if(!create_dir(output_pre_with_path + output_archive_without_path)){
        return false;
    }
    if(!create_dir(output_pre_with_path + output_tags_without_path)){
        return false;
    }

    return true;
}


static bool create_nginx_blog_without_md(string md_file_dir_with_path,
                                         string html_title,
                                         string blog_name,
                                         string blog_subname,
                                         int max_item_per_page,
                                         blog_home_page* blog_home){


    bool ret = blog_home->get_blog_dir(md_file_dir_with_path);
    if(!ret){
        cout << "can not get blog dir " << md_file_dir_with_path << endl;
        return false;
    }

    ret = blog_home->get_blog_files_attr();
    if(!ret){
        cout << "can not get all blog files attr\n";
        return false;
    }

    ret = blog_home->create_blog_header(html_title,blog_name,blog_subname);
    if(!ret){
        cout << "can not create blog header\n";
        return false;
    }

    ret = blog_home->create_blog_left_sidebar();
    if(!ret){
        cout << "can not create blog left sidebar\n";
        return false;
    }

    ret = blog_home->create_blog_right_sidebar();
    if(!ret){
        cout << "can not create blog right sidebar\n";
        return false;
    }

    ret = blog_home->create_blog_ending();
    if(!ret){
        cout << "can not create blog ending\n";
        return false;
    }


    ret = blog_home->create_blog_index_contents(max_item_per_page);
    if(!ret){
        cout << "can not create index contents\n";
        return false;
    }

    ret = blog_home->create_blog_archive_contents(max_item_per_page);
    if(!ret){
        cout << "can not create archive contents\n";
        return false;
    }

    ret = blog_home->create_blog_tags_contents(max_item_per_page);
    if(!ret){
        cout << "can not create tags contents\n";
        return false;
    }

    ret = blog_home->create_blog_index_page();
    if(!ret){
        cout << "can not create blog index page\n";
        return false;
    }

    ret = blog_home->create_blog_archive_page();
    if(!ret){
        cout << "can not create blog archive page\n";
        return false;
    }

    ret = blog_home->create_blog_tags_page();
    if(!ret){
        cout << "can not create blog tags page\n";
        return false;
    }

    return true;
}

static bool output_index_html(string output_dir_with_path,
                              int max_item_per_page,
                              blog_home_page* blog_home){

    //create index html

    ofstream out_file;
    string out_file_name;
    string index = string("index");
    string post = string(".html");

    vector<string>::iterator it;
    int i = 0;
    int max_page = blog_home->blog_files.size() / max_item_per_page;
    if((blog_home->blog_files.size() % max_item_per_page) > 0 ){
        max_page = max_page + 1;
    }

    for(it = blog_home->html_index_page_v.begin();
        it != blog_home->html_index_page_v.end();
        it++){
        if(i == 0){
            out_file_name = output_dir_with_path + index + post;
            out_file.open(out_file_name);
        }
        else{
            out_file_name = output_dir_with_path + index + string("_")
                    + to_string(i + 1) + post;
            out_file.open(out_file_name);
        }

        out_file << (*it);
        out_file.close();
        if((i+1) == max_page){
            out_file_name = output_dir_with_path + index + string("_last") + post;
            out_file.open(out_file_name);
            out_file << (*it);
            out_file.close();
        }
        i = i + 1;
    }

    return true;
    //end create index html


}

static bool output_archive_html(string output_dir_with_path,
                                int max_item_per_page,
                                blog_home_page* blog_home){

    //create archive html
    ofstream out_file;
    string out_file_name;
    string post = string(".html");

    map<string,string,less<string> >::iterator itm;

    int max_page = blog_home->blog_files.size() / max_item_per_page;
    if((blog_home->blog_files.size() % max_item_per_page) > 0 ){
        max_page = max_page + 1;
    }

    for(itm = blog_home->html_archive_page_vv.begin();
        itm != blog_home->html_archive_page_vv.end();
        itm++){

        out_file_name = output_dir_with_path + itm->first + post;
        out_file.open(out_file_name);

        out_file << itm->second;
        out_file.close();
    }
    return true;
    //end create archive html
}

static bool output_tags_html(string output_dir_with_path,
                             blog_home_page* blog_home){
    //create tag html

    ofstream out_file;
    string out_file_name;
    string post = string(".html");

    map<string,string,less<string> >::iterator itm;

    for(itm = blog_home->html_tags_page_vv.begin();
        itm != blog_home->html_tags_page_vv.end();
        itm++){

        out_file_name = output_dir_with_path + itm->first + post;
        out_file.open(out_file_name);

        out_file << itm->second;
        out_file.close();
    }

    return true;
    //end create tag html
}

static bool create_output_all_md_blog_html(string output_dir_with_path,
                                           string html_title,
                                           string blog_name,
                                           string blog_subname,
                                           blog_home_page* blog_home){

    //create all html

    ofstream out_file;
    string out_file_name;
    string post = string(".html");
    string html_name;
    string html_contents;
    multimap<string,blog_file_attr_t,less<string> >::iterator itd;
    int pos = 0;
    for(itd = blog_home->tags_mmap_blog_file_attr.begin();
        itd != blog_home->tags_mmap_blog_file_attr.end();
        itd++){
        html_name = itd->second.blog_file_time.blog_file_name;
        pos = html_name.find_last_of(".");
        out_file_name = output_dir_with_path + html_name.substr(0,pos) + post;
        out_file.open(out_file_name);

        if(blog_home->create_md_header(html_title,
                                       blog_name,
                                       blog_subname,
                                       itd->second)){
            html_contents = blog_home->html_md_header;
        }
        else{
            cout << "error: create md header " << itd->second.blog_file_time.blog_file_name << endl;
        }

        if(blog_home->create_md_contens(itd->second)){
            html_contents = html_contents +
                    string("<div class=\"main-page-box\">\n") +
                    blog_home->html_md_contents +
                    blog_home->html_md_catalog +
                    blog_home->html_right_sidebar + string("</div>\n")
                    + blog_home->html_ending;
        }
        else{
            cout << "error 1: create md contents " << itd->second.blog_file_time.blog_file_name << endl;
        }

        out_file << html_contents;
        out_file.close();
    }

    return true;
    //end create all html
}



static bool create_out_a_md_blog_html(string src_dir_with_path,
                                      string md_name_with_post,
                                      string html_title,
                                      string blog_name,
                                      string blog_subname,
                                      string dst_dir_with_path,
                                      blog_home_page* blog_home){

    //create md html
    if(md_name_with_post.empty()){
        return true;
    }

    string post = string(".html");
    string html_name;
    string html_contents;
    int pos = 0;

    html_name = md_name_with_post;
    pos = html_name.find_last_of(".");
    string out_file_name = dst_dir_with_path + html_name.substr(0,pos) + post;
    ofstream out_file;
    out_file.open(out_file_name);

    blog_file_attr_t bfa;

    if(!blog_home->get_a_blog_file_attr(&bfa,(src_dir_with_path + md_name_with_post))){
        cout << "can not get the attr of " << (src_dir_with_path + md_name_with_post) << endl;
        out_file.close();

        return false;
    }


    if(blog_home->create_md_header(html_title,
                                   blog_name,
                                   blog_subname,
                                   bfa)){
        html_contents = blog_home->html_md_header;
    }
    else{
        cout << "error:  when create md header of " << src_dir_with_path + md_name_with_post << endl;
        out_file.close();

        return false;
    }



    if(blog_home->create_md_contens(bfa)){
        html_contents = html_contents +
                string("<div class=\"main-page-box\">\n") +
                blog_home->html_md_contents +
                blog_home->html_md_catalog +
                blog_home->html_right_sidebar + string("</div>\n")
                + blog_home->html_ending;
    }
    else{
        cout << "error :  when create md contents of" << src_dir_with_path + md_name_with_post << endl;
        out_file.close();
        return false;
    }

    out_file << html_contents;
    out_file.close();

    return true;
    //end create md html
}



static bool create_out_about_me_html(string src_dir_with_path,
                                      string md_name_with_post,
                                      string html_title,
                                      string blog_name,
                                      string blog_subname,
                                      string dst_dir_with_path,
                                      blog_home_page* blog_home){

    //create md html
    string post = string(".html");
    string html_name;
    string html_contents;
    int pos = 0;
    

    html_name = md_name_with_post;
    html_title = "关于我";
    pos = html_name.find_last_of(".");
    string out_file_name = dst_dir_with_path + html_name.substr(0,pos) + post;
    ofstream out_file;
    out_file.open(out_file_name);

    blog_file_attr_t bfa;

    if(!blog_home->get_a_blog_file_attr(&bfa,(src_dir_with_path + md_name_with_post))){
        cout << "can not get the attr of " << (src_dir_with_path + md_name_with_post) << endl;
        out_file.close();

        return false;
    }

    bfa.blog_file_time.blog_file_name = string("about_me.md");
    if(blog_home->create_md_header(html_title,
                                   blog_name,
                                   blog_subname,
                                   bfa)){
        html_contents = blog_home->html_md_header;
    }
    else{
        cout << "error:  when create md header of " << src_dir_with_path + md_name_with_post << endl;
        out_file.close();

        return false;
    }

 

    if(blog_home->create_md_contens(bfa)){
        blog_home->html_right_sidebar = string("<div class=\"right-sidebar\">"
                                "<h3> </h3>\n</div>");
	blog_home->html_md_catalog = string("<div class=\"left-sidebar\">"
			        "<h3> </h3>\n</div>");
        html_contents = html_contents +
                string("<div class=\"main-page-box\">\n") +
                blog_home->html_md_contents +
                blog_home->html_md_catalog +
                blog_home->html_right_sidebar + string("</div>\n")
                + blog_home->html_ending;
   }
    else{
        cout << "error :  when create md contents of" << src_dir_with_path + md_name_with_post << endl;
        out_file.close();
        return false;
    }

    out_file << html_contents;
    out_file.close();

    return true;
    //end create md html
}




























